<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of system_controller
 *
 * @author phamhphuc
 */
class system_controller {
    //put your code here
    protected static $_instance = null;
    public $config;
    public $action = "index";
    public $isUseLayout = true;
    public $layoutName = "default";
    public $forwardAction = "";    
    /**
     *
     * @var type system_view
     */
    public $view = null;
    /**
     *
     * @var type system_session
     */
    public $session = null;
    /**
     *
     * @var type system_db
     */
    public $db;
    
    public function __construct($config) {              
        $this->config = $config;        
        if(isset($config['default_action'])) {
            $this->action = $config['default_action'];
        }
        $this->view = new system_view();        
        $this->session = new system_session($config);
        $this->db = new system_db($config);               
        
        $this->_setupData();        
        $this->init();
    }        
    
    public function init()
    {
        $this->view->config = $this->config;
    }    
    
    protected function setlayout($layoutName) {
        $this->layoutName = $layoutName;
    }
    
    public function handle()
    {        
        if(isset($_REQUEST['action'])) {
            $this->action = $_REQUEST['action'];
        }
        
        $content = "";
        
        $actionName = $this->action . "Action";
        $content = $this->_callAction($actionName);
        
        if($this->isUseLayout) {            
            
            $data = $this->view->getData();
            
            $data['view_content'] = $content;
            
            $data = (object)$data;
            
            $data = array(
                'view' => $data
            );  
            
            $html = $this->get_include_contents("{$this->config['template_dir']}layouts/{$this->layoutName}.php", $data);
            echo $html;
        } else {
            echo $content;
        }        
    }
    
    
    protected function _forward($actionName) {
        $this->forwardAction = $actionName;
    }


    public function _callAction($actionName) {               
        $this->$actionName();
        $this->view->action = $this->action;
        if($this->forwardAction != "") {
            $this->action = $this->forwardAction;
            $this->forwardAction = "";
            $actionName = $this->action . "Action";
            $this->$actionName();
            $this->view->action = $this->action;
        }
        $data = $this->view->getData();
        
        $data['action'] = $this->action;
        
        $data = (object)$data;
        $data = array(
            'view' => $data
        );        
        $viewName = str_replace("Action", "", $actionName);
                
        $content = $this->get_include_contents("{$this->config['template_dir']}views/{$viewName}.php", $data);
        return $content;
    }
    
    public function getTemplateDir(){
        if($this->config['template_name']) {
            return "../../templates/{$this->config['template_name']}/";
        }
        return "";
    }
    
    public function getTemplateURL(){
        if($this->config['template_name']) {
            return "../../templates/{$this->config['template_name']}/";
        }
        return "";
    }

    public static function getInstance() {
        if (null === self::$_instance) {
            self::$_instance = new self ();
        }
        return self::$_instance;
    }
    
    public function _setNoLayout()
    {
        $this->isUseLayout = false;
    }
    
    public function get_include_contents($filename , $data = '') {        
        if (is_file($filename)) {            
            if (is_array($data)) {
                extract($data);
            }
            ob_start();
            include $filename;
            $contents = ob_get_contents();
            ob_end_clean();
            return $contents;
        } else {
            die($filename . " doesn't exists");
        }        
    }
    
    private function _setupData()
    {                
        $this->config['template_dir'] = $this->getTemplateDir();
        $this->config['template_url'] = $this->getTemplateURL();
    }
}

?>
